package com.killer.service;

import com.killer.dao.PersonRepository;
import com.killer.pojo.Person;
import com.killer.vo.PersonEdge;
import com.killer.vo.PersonNode;
import com.killer.vo.PersonVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：Killer
 * @date ：Created in 21-2-7 下午3:28
 * @description：${description}
 * @modified By：
 * @version: version
 */
@Service
public class PersonService {

    @Autowired
    private PersonRepository personRepository;

    public PersonVo listRelatePerson(String startName, String endName) {
        PersonVo personVo=new PersonVo();
        //查询所有路径
        List<Person> personList = personRepository.listPath(startName, endName);
        if (personList == null || personList.isEmpty()) {
            return personVo;
        }
        //封装节点
        List<PersonNode> nodes = new ArrayList<PersonNode>();
        for(int i=0;i<personList.size();i++) {
            PersonNode personNode=new PersonNode();
            personNode.setNum(i);
            personNode.setName(personList.get(i).getName());
            nodes.add(personNode);
        }
        personVo.setNodes(nodes);
        //封装边
        List<PersonEdge> edges = new ArrayList<PersonEdge>();
        for (PersonNode startNode : nodes) {
            for (PersonNode endNode : nodes) {
                if (startNode.getNum() == endNode.getNum()) {
                    //等于自己则跳过
                    continue;
                }
                //判断这两个节点是否有关联
                List<Person> checkPersons = personRepository.checkRelate(startNode.getName(), endNode.getName());
                if (checkPersons == null || checkPersons.isEmpty()) {
                    //没有关联，则跳过
                    continue;
                }
                //构建关联边
                PersonEdge personEdge=new PersonEdge(startNode.getNum(),endNode.getNum());
                edges.add(personEdge);
            }
        }
        personVo.setEdges(edges);
        return personVo;
    }

}
